<template>
  <a-drawer
    title="Create a new account"
    :visible="visible"
    :title="title"
    :maskClosable="true"
    width="60%"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :destroyOnClose="true"
  >
    <h2>项目部管理</h2>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" >
        <a-form-item
          v-show="false"
          label="id"
        >
          <a-input
            :disabled="showable"
            v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="客户编码"
          class='form-item-left'
        >
          <a-input
            placeholder='请输入客户编码'
            :disabled="showable||editable"
            v-decorator="['customersCode', {rules: [{required: true, message: '请输入客户编码'}]}]" />
        </a-form-item>
        <a-form-item
          label="客户名称"
          class='form-item-right'
        >
          <a-input
            :disabled="showable"
            placeholder='请输入客户名称'
            v-decorator="['customersName', {rules: [{required: true, message: '请输入客户名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="统一社会信用代码"
          class='form-item-left'
        >
          <a-input
            :disabled="showable"
            placeholder='请输入统一社会信用代码'
            v-decorator="['unifiedSocialCode']" />
        </a-form-item>
        <a-form-item
          label="银行名称"
          class='form-item-right'

        >
          <a-input
            placeholder='请输入银行名称'
            :disabled="showable"
            v-decorator="['bankName']" />
        </a-form-item>
        <a-form-item
          label="银行账户"
          class='form-item-left'

        >
          <a-input
            placeholder='请输入银行账户'
            :disabled="showable"
            v-decorator="['bankId']" />
        </a-form-item>
        <a-form-item
          label="法人"
          class='form-item-right'

        >
          <a-input
            placeholder='请输入法人姓名'
            :disabled="showable"
            v-decorator="['legalPerson']" />
        </a-form-item>
        <a-form-item
          label="电话"
          class='form-item-left'

        >
          <a-input
            placeholder='请输入电话'
            :disabled="showable"
            v-decorator="['phone']" />
        </a-form-item>
        <a-form-item
          label="地址"
          class='form-item-right'

        >
          <a-input
            placeholder='请输入详细地址'
            :disabled="showable"
            v-decorator="['place']" />
        </a-form-item>
        <a-form-item
          label="邮箱"
          class='form-item-left'

        >
          <a-input
            placeholder='请输入邮箱'
            :disabled="showable"
            v-decorator="['mailbox']" />
        </a-form-item>
        <a-form-item
          label="QQ号"
          class='form-item-right'

        >
          <a-input
            placeholder='请输入QQ账号'
            :disabled="showable"
            v-decorator="['qq']" />
        </a-form-item>
        <a-form-item
          label="客户联系人"
          class='form-item-left'

        >
          <a-input
            placeholder='请输入客户联系人'
            :disabled="showable"
            v-decorator="['contact']" />
        </a-form-item>
        <a-form-item
          label="联系电话"
          class='form-item-right'

        >
          <a-input
            placeholder='请输入客户联系电话'
            :disabled="showable"
            v-decorator="['contactPhone']" />
        </a-form-item>
        <a-form-item
          label="联系人性别"
          class='form-item-left'
        >
          <a-select
            showSearch
            :disabled="showable"
            :options="sexList"
            allowClear="true"
            :filterOption="filterOption"
            optionFilterProp="children"
            v-decorator="['contactSex']" />
        </a-form-item>
        <a-form-item
          label="状态"
          class='form-item-right'
        >
          <a-radio-group v-decorator="['status']"  :disabled="showable" :default-value="0">
            <a-radio :value="0">
              启用
            </a-radio>
            <a-radio :value="1">
              停用
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="备注 "
          class='form-item-left'
        >
          <a-textarea
            placeholder='请输入备注'
            :disabled="showable"
            v-decorator="['remarks', {rules: [{required: false, message: '请输入备注'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>

    <div v-show="editable || addable"
         class="drawer-bootom-button">
      <a-button key="cancel" @click="visible = false">取消</a-button>
      <a-button key="forward" :loading="confirmLoading" type="primary" style='margin-left: 15px' @click="handleOk">保存</a-button>
    </div>

  </a-drawer>
</template>

<script>
import { FormMixin } from '@/mixins/FormMixin'
import pick from 'lodash.pick'
import { addObj, getObj, putObj } from '@/api/base/customerBase'
import { dropDownByCode } from '@api/sys/sys'


export default {
  name: 'customerAddOrUpdate',
  mixins: [FormMixin],
  data() {
    return {
      confirmLoading: false,
      sexList:[],
      form: this.$form.createForm(this),
      visible: false,
      title:null
    }

  },
  methods: {
    edit(id, type) {
      console.log(type,"类型")
      if(type === 'add'){
        this.title = '新建'
      }else if(type === 'edit'){
        this.title = '编辑'
      }else if(type === 'show') {
        this.title = '查看'
      }else{
        this.title = null
      }
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      //联系人性别
      dropDownByCode('contact_sex').then(res => {
        this.sexList = res.result
      })
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          if (record.category === 'lineSite') {
            this.typeChange(record.category)
          }
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'customersCode', 'contactPhone','customersName', 'unifiedSocialCode','bankName','bankId','qq','mailbox','contact','contactSex', 'legalPerson','place', 'remarks', 'status']))
          })
        })
      } else {
        resetFields()
      }
    },
    // 过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    //点击保存，执行的方法
    handleOk() {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true

          if (this.type === 'add') {
            addObj(values).then(res => {
              if (res.code !== 200 && res.code !== 0) {
                this.confirmLoading = false
                this.$message.error(res.message)
              } else {
                this.confirmLoading = false
                this.$message.success('操作成功')
                this.$emit('ok')
                this.handleCancel()
              }
            })
          } else if (this.type === 'edit') {
            putObj(values).then(res => {
              if (res.code !== 200 && res.code !== 0) {
                this.confirmLoading = false
                this.$message.error(res.message)
              } else {
                this.confirmLoading = false
                this.$message.success('操作成功')
                this.$emit('ok')
                this.handleCancel()
              }

            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>
  .form-item-left{
    width:48%;
    display: inline-block;

  }
  .form-item-right{
    width:48%;
    display: inline-block;
    float: right;
  }
  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>